//
//  VerticalLabel.swift
//  HLCalc
//
//  Created by 黄龙 on 2023/10/8.
//

import UIKit

enum VerticalAlignment {
    case VerticalAlignmentTop,VerticalAlignmentMiddle,VerticalAlignmentBottom
    
}


class VerticalLabel: UILabel {
    private var verticalAlignment:VerticalAlignment = .VerticalAlignmentMiddle
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.verticalAlignment = .VerticalAlignmentMiddle
    }
    
    func setVerticalAlignment(_ alignment: VerticalAlignment){
        self.verticalAlignment = alignment
        self.setNeedsDisplay()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func textRectForBounds(bounds:CGRect,numberOfLines:Int)->CGRect{
        var textArea = super.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
        switch self.verticalAlignment {
        case .VerticalAlignmentTop:
            textArea.origin.y = self.bounds.origin.y
            break
        case .VerticalAlignmentMiddle: //UILabel默认就是垂直居中
            break
        case .VerticalAlignmentBottom:
            textArea.origin.y = bounds.origin.y + bounds.size.height - textArea.size.height
            break
//        default:
//            textArea.origin.y = bounds.origin.y + (bounds.size.height - textArea.size.height) / 2
        }
        return textArea
    }
    
    override func drawText(in rect: CGRect) {
        let actualRect = self.textRectForBounds(bounds: rect, numberOfLines: self.numberOfLines)
        super.drawText(in: actualRect)
        
    }
    
}
